#
#-----[ COPY ]------------------------------------------
#
make_cookie_tornetua.php to /parser_mod/tools/make_cookie_tornetua.php

#
#-----[ OPEN ]------------------------------------------
#
parser.php

#
#-----[ FIND ]------------------------------------------
#
	preg_match("#(?:(rutracker\.org)\/forum

#
#-----[ IN-LINE FIND ]------------------------------------------
#
(?:(lostfilm\.tv)\/details\.php\?id=\d+)

#
#-----[ IN-LINE AFTER ADD ]------------------------------------------ 
# 
|(?:(torrents\.net\.ua)\/forum\/viewtopic\.php\?t=\d+)

#
#-----[ FIND ]------------------------------------------
#
	    case "lostfilm.tv":  $source_id="3"; break;

#
#-----[ AFTER ADD ]------------------------------------------
#
	    case "torrents.net.ua":  $source_id="4"; break;

#
#-----[ OPEN ]------------------------------------------
#
parser_mod/includes/function_parser.php

#
#-----[ FIND ]------------------------------------------
#
		case 3:$parse_data = parse_lost_topic($address); break;

#
#-----[ AFTER ADD ]------------------------------------------
#
		case 4:$parse_data = parse_tornetua_topic($address); break;

#
#-----[ FIND ]------------------------------------------
#
function parse_tru_topic($url)

#
#-----[ BEFORE ADD ]------------------------------------------
#
function parse_tornetua_topic($url)
{
        global $board_config, $torrent_place, $cookie_place;
	$text = $phys_name = $real_name = $bytes = $error_msg = "";
	$cookie_file_path = $cookie_place."cookie_tornetua.txt";

// -
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
	$page = curl_exec ($ch);
	curl_close ($ch);

	preg_match ("#<tbody id=\"post_(\d+)\" class=\"row1\">#si", $page, $r_post);
	$q_url = "http://torrents.net.ua/forum/posting.php?mode=quote&p=".$r_post[1];

	preg_match ("#<title>(.*?)::.*?</title>#si", $page, $r_title);
	preg_match ("#(download.php\?id=\d+)\" class=\"genmed\"><b>Download#si", $page, $r_torrent);

	$torrent = "http://torrents.net.ua/forum/".$r_torrent[1];
	    
	$error_msg = '';

	if ($r_torrent[1]){

		$title = $r_title[1];
		$title = encodestring($title);

		$t_name = strip_tags($title);
		$t_name = str_replace(" ", "_", $t_name);
		$t_name = str_replace("/", "-", $t_name);

		$phys_name = (( strlen($t_name) > 50 ) ? substr($t_name, 0, 50): $t_name) ."[".rand(100000, 999999)."].torrent";
		$real_name = (( strlen($t_name) > 100 ) ? substr($t_name, 0, 100): $t_name) ."_[".$board_config['cookie_domain']."].torrent";


//   
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $torrent);
                curl_setopt($ch, CURLOPT_REFERER, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "foo=bar"); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
		$torrent_data = curl_exec ($ch);
		curl_close ($ch); 

		$filename = $torrent_place.$phys_name;
	
		$f = @fopen($filename, 'w');
	
		  if ($f) 
		     {
		          $bytes = fwrite($f, $torrent_data);
		          fclose($f);
		     } else return $parse_data[4] = "   . (     )";
//  
		$search = "#<div class=\"post_wrap\">(.*?)<table class=\"attach bordered med\">#si";
		preg_match ($search, $page, $matches);
		$post = $matches[1];

		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $q_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
		$q_page = curl_exec ($ch);
		curl_close ($ch);

		$search = "#<textarea.*?name=\"message\".*?>\[quote=\".*?\"\](.*?)\[\/quote\]<\/textarea>#si";
		preg_match ($search, $q_page, $matches);
		$text = $matches[1];	
		$text = preg_replace("#(?:\[br\])#si", "\r\n", $text);
		$text = preg_replace("#(?:\[tab\])#si", "", $text);

//  
		if (preg_match("#\[thumb\].*\[\/thumb\]#si", $text, $thumbs_code)) {
			preg_match_all("#<a target=_blank href=\"(.*?)\" class=\"lightbox\"#si",$post, $thumbs);
			$sreenshots = "[spoiler=\"\"]".get_screenshots($thumbs[1], "thumbs")."[/spoiler]";
			$text = str_replace($thumbs_code[0], $sreenshots, $text);
		}
//   
		while (preg_match("#<p class=\"attach_link\">.*?<a href=\"./(download.php\?id=\d+)\" ><b>(.*?)</b></a>#si", $post, $attach)) {
			$text .= " : [url=http://torrents.net.ua/forum/".$attach[1]."]".$attach[2]."[/url]";
			$post = preg_replace("#".preg_quote($attach[0])."#si", " ", $post);
		}

//    
		preg_match_all("#\[img(?:=.*?)?\](http://torrents.net.ua/.*?)\[/img\]#si", $text, $tornetua_img);
		$posters_etc = get_screenshots($tornetua_img[1]);
		$text = str_replace($tornetua_img[1], $posters_etc,$text);

	// 
		$text .= "[hr][b][/b]: [url=".$url."][img]http://www.toptracker.ru/banners/40.gif[/img][/url]";

//		$text = strip_tags($text);
	}else {
			$error_msg = '    .    .';
	}

//  return
	$parse_data[0] = $text;
	$parse_data[1] = $phys_name;
	$parse_data[2] = $real_name;
	$parse_data[3] = $bytes;
	$parse_data[4] = $error_msg;
	$parse_data[5] = $r_title[1];

	return $parse_data;

}

function get_screenshots($urls_array, $mode="") {
        global $board_config, $torrent_place, $cookie_place, $photos_place, $uri_photos_place;
	$cookie_file_path = $cookie_place."cookie_tornetua.txt";
	$dwnld_urls ='';
	$uri_add = ($mode == "thumbs")? 'http://torrents.net.ua':'';

	$node_count = count($urls_array);
	$curl_arr = array();
	$master = curl_multi_init();

	for($i = 0; $i < $node_count; $i++)
	{
		$urls_array[$i] = $uri_add.$urls_array[$i];
		$curl_arr[$i] = curl_init($urls_array[$i]);
		curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, 1); 
	        curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		curl_setopt($curl_arr[$i], CURLOPT_COOKIEFILE, $cookie_file_path);
		curl_setopt($curl_arr[$i], CURLOPT_COOKIEJAR, $cookie_file_path);
		curl_multi_add_handle($master, $curl_arr[$i]);
	}
	
	do {
	    curl_multi_exec($master,$running);
	} while($running > 0);


	for($i = 0; $i < $node_count; $i++)
	{
		$results[$urls_array[$i]] = curl_multi_getcontent  ( $curl_arr[$i]  );
		curl_multi_remove_handle($master, $curl_arr[$i]);
	}
	curl_multi_close($master);

	foreach ($results as $key => $value) {
		$type = substr($key, strrpos($key, '.') + 1);
		$photo_name = time().rand().".".$type;
		file_put_contents($photos_place.$photo_name, $value);
		$dwnld_urls .= " [img]".$uri_photos_place.$photo_name."[/img]";
		$id = array_search($key, $urls_array);
		$dwnld_urls_array[$id] = $uri_photos_place.$photo_name;
	}
	$dwnld_urls_data = ($mode == "thumbs")? $dwnld_urls:$dwnld_urls_array;
	return $dwnld_urls_data;
}

#
#-----[ OPEN ]------------------------------------------
#
parser_mod/config.php

#
#-----[ FIND ]------------------------------------------
#
$login_data = array();

#
#-----[ AFTER ADD ]------------------------------------------
#
/*  ,    ,    , :  "/server/mysite/forum/data/photos/" */
$photos_place = "";

/*  ,    ,    : "http://mysite.ru/data/photos/"  */
$uri_photos_place = "";

#
#-----[ FIND ]------------------------------------------
#
$login_data['lostfilm']['password']

#
#-----[ AFTER ADD ]------------------------------------------
#
$login_data['tornetua']['username'] = "";
$login_data['tornetua']['password'] = "";


#
#-----[ DIY INSTRUCTIONS ]------------------------------------------ 
#   config.php    torrents.net.ua       (  )
edit parser_mod/config.php
run parser_mod/tools/make_cookie_tornetua.php

# 
# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM
